﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConfigCreate;
using Newtonsoft.Json;
using System.Diagnostics;
using ConfigModel;
using System.Dynamic;
using System.ComponentModel;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ModelData mdata = new ModelData();
        private DynamicBindingRoutePath[] dynamic_bindings = null;
        private ConfigData config_data = new ConfigData();

        public MainWindow()
        {
            InitializeComponent();

            //注册自定义控件类型绑定
            DataTemplateManager.RegisterDataTemplate<ConfigModel.ViewModel.MyUControlModel, ConfigModel.View.MyUControl>();
        }

        private void Win_Manual_Click(object sender, RoutedEventArgs e)
        {
            //声明配置框
            ConfigWin win = new ConfigWin();
            //设置配置框标题
            win.Title = "我的配置框";
            //设置配置框图标
            win.Icon = new BitmapImage(new Uri("pack://application:,,,/WpfTest;component/爆米花.png"));
            //设置参数修改通知
            win.SaveParametersChange += Win_SaveParametersChange;

            //添加文本框
            win.AddTextBox("基本设置", "主要参数", mdata, "MyTitle", "文本框", necessary: true);
            //添加数字框
            win.AddNumeric("基本设置", "主要参数", mdata, "MyNumber", "数值框");
            //添加切换框-带文字
            win.AddToggleButton("基本设置", "第二参数", mdata, "MyBool", "切换按钮1", "选择", "不选");
            //添加切换框-默认样式
            win.AddToggleButton("基本设置", "第二参数", mdata, "MyBool", "切换按钮2");
            //添加密码框
            win.AddPasswordBox("基本设置", "第二参数", mdata, "MyPassWord", "密码框");
            //设置下拉框 项列表
            List<ComboBoxData> tl = new List<ComboBoxData>()
            {
                new ComboBoxData("这是第一项", TData.One),
                new ComboBoxData("第二项啊-第二项", TData.Two),
                new ComboBoxData("不会是第三项吧", TData.Three),
                new ComboBoxData("最后的第四项了",TData.Four)
            };
            //添加下拉框
            win.AddComboBox("进阶设置", "第一组", mdata, "MyTData", "组合框", typeof(TData), tl);
            //添加日期框
            win.AddDatePicker("进阶设置", "第一组", mdata, "MyDate", "日期选择框");
            //添加日期时间框
            win.AddDateTimePicker("进阶设置", "第一组", mdata, "MyTime", "日期时间选择框");
            //添加多选下拉框
            win.AddMultipleComboBox("进阶设置", "第二组", mdata, "MyList", "多选组合框", typeof(TData), tl);
            //添加IP地址设置框
            win.AddIpAddressTextBox("进阶设置", "第二组", mdata, "MyIP", "IP地址框");
            //添加文件选择框 - 单选
            win.AddOpenFile("进阶设置", "第二组", mdata, "SelectedFile", "单文件选择框", "请选择要载入的文本文件", "文本文件|*.txt|所有文件|*.*");
            //添加文件选择框 - 多选
            win.AddOpenFile("进阶设置", "第二组", mdata, "Files", "多文件选择框", "请选择要载入的文本文件", "文本文件|*.txt|所有文件|*.*", true);
            //添加文件保存框
            win.AddSaveFile("进阶设置", "第二组", mdata, "SaveFile", "文件保存框", "请选择要保存的文本文件", "文本文件|*.txt|所有文件|*.*", true);
            //添加目录选择框
            win.AddOpenFolder("进阶设置", "第二组", mdata, "MyFolder", "目录选择框", "请选择打开的目录");
            //添加标签框
            win.AddTextBlock("进阶设置", "第三组", mdata, "MyLabel", horizontal_alignment: HorizontalAlignment.Center, font_weight: "Bold", font_size: 28);
            //添加自定义控件
            win.AddCustomControl("基本设置", "第二参数", mdata, "MyUControlModel");

            //设置可见关联
            win.SetVisibilityPanel("基本设置", "第二参数", "MyPassWord", "MyBool", true);
            win.SetVisibilityPanel("进阶设置", "第二组", "MyFolder", "基本设置", "第二参数", "MyBool", true);
            win.SetVisibilityGroup("基本设置", "主要参数", "基本设置", "第二参数", "MyBool", true);

            //设置权限(设置权限角色)
            win.SetRolePermissionPanel("进阶设置", "第一组", "MyTData", "管理员");
            win.SetRolePermissionPanel("进阶设置", "第一组", "MyTData", "主管");
            win.SetRolePermissionPanel("进阶设置", "第一组", "MyTData", "管理员", RolePermissionType.UnEnabled);
            win.SetRolePermissionGroup("进阶设置", "第二组", "管理员");
            win.SetRolePermissionGroup("进阶设置", "第二组", "主管");
            win.SetRolePermissionGroup("进阶设置", "第二组", "管理员", RolePermissionType.UnEnabled);
            win.SetRolePermissionHeader("基本设置", "管理员");
            win.SetRolePermissionHeader("基本设置", "主管");
            win.SetRolePermissionHeader("基本设置", "管理员", RolePermissionType.UnEnabled);

            //设置数值变更通知
            win.SetEditNotificationPanel("进阶设置", "第二组", "Files", "我的选择文件");
            win.SetEditNotificationPanel("进阶设置", "第二组", "SaveFile", "保存的文件路径");

            //设置绑定模式
            win.AddOrSetBindingPanel("基本设置", "主要参数", "MyNumber", "节点B.字符串1", (obj, gh) => GetBindingDatas(), false, true, necessary: true);
            //未设置控件，只设置绑定项,并且为必填项
            win.AddOrSetBindingPanel("进阶设置", "第三组", "Txt", "", (obj, gh) => GetBindingDatas(), true, false, "文本", necessary: true);

            win.DynamicBindingNotification = (dbs) =>
            {
                dynamic_bindings = dbs;
                Debug.WriteLine($"获得绑定字典：【{JsonConvert.SerializeObject(dbs)}】");
            };
            //显示配置框，传入当前登陆角色
            if (win.ShowDialogWin(true,"管理员", dynamic_bindings) == true)
            {

            }
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        /// <returns></returns>
        private List<BindingData> GetBindingDatas()
        {
            //生成绑定列表
            List<BindingData> list = new List<BindingData>();
            BindingData bdata = new BindingData()
            {
                NodeName = "节点A",
                ChildNodes = new List<BindingData>()
            };
            bdata.ChildNodes.Add(new BindingData() { NodeName = "数字1", RoutePath = "节点A.数字1" });
            bdata.ChildNodes.Add(new BindingData() { NodeName = "字符串1", RoutePath = "节点A.字符串1" });
            bdata.ChildNodes.Add(new BindingData() { NodeName = "数字2", RoutePath = "节点A.数字2" });
            list.Add(bdata);
            //
            bdata = new BindingData()
            {
                NodeName = "节点B",
                ChildNodes = new List<BindingData>()
            };
            bdata.ChildNodes.Add(new BindingData() { NodeName = "日期", RoutePath = "节点B.日期" });
            bdata.ChildNodes.Add(new BindingData() { NodeName = "字符串1", RoutePath = "节点B.字符串1" });
            bdata.ChildNodes.Add(new BindingData() { NodeName = "数字1", RoutePath = "节点B.数字1" });
            list.Add(bdata);
            return list;
        }

        private void Win_SaveParametersChange(object sender, ParameterChange[] e)
        {
            foreach (var pc in e)
            {
                Debug.WriteLine($"【{pc.Definition}】从({JsonConvert.SerializeObject(pc.Origin)})变更为({JsonConvert.SerializeObject(pc.Target)}), 类型为：{pc.ParameterType}");
            }
        }

        private void Win_Simple_Click(object sender, RoutedEventArgs e)
        {
            //生成简单配置框
            SimpleData data = new SimpleData("Node1", "节点1");
            ConfigWin win = new ConfigWin();
            win.Width = 350;
            win.Height = 200;
            win.Title = "修改节点属性";
            if (win.AddControlSource(data))
                win.ShowDialogWin();

            MessageBox.Show(JsonConvert.SerializeObject(data));
        }

        private void Win_Object_Click(object sender, RoutedEventArgs e)
        {
            //配置绑定列表
            config_data.BindingList = GetBindingDatas();

            //
            ConfigWin win = new ConfigWin();
            win.Title = "我的配置框";
            win.SaveParametersChange += Win_SaveParametersChange;

            //设置配置框绑定对象并打开配置框，设置配置文本
            if (win.AddControlSource(config_data, config_file: "ConfigData.json"))
                win.ShowDialogWinWithConfigFile("Binding.json"); //绑定还原配置文件
        }

        private void Win_Dock_Click(object sender, RoutedEventArgs e)
        {
            //停靠配置类，可以设置各个组的停靠
            DockData data = new DockData(); //该类中有生成各组停靠顺序解说
            ConfigWin win = new ConfigWin() { Title = "排列窗口" };
            win.Width = 800;
            win.Height = 500;
            if (win.AddControlSource(data))
            {
                win.SetPanelTitleWidht(60); //设置控件标题宽度统一为60
                win.ShowDialogWin();
            }
        }

        private void Win_Dymanic_Click(object sender, RoutedEventArgs e)
        {
            dynamic obj = new ExpandoObject();
            //设置这个属性可使UI更新时可更新到原对象上
            obj.Txt = "ABCDEFG";
            obj.Number = 123456;
            ConfigWin win = new ConfigWin();
            win.Title = "动态类绑定";
            win.AddTextBox("", "", obj, "Txt", "文本", necessary: true);
            win.AddNumeric("", "", obj, "Number", "数字", necessary: true);
            win.ShowDialogWin(true);
            string message = $"Txt: {obj.Txt}, Number: {obj.Number}";
            MessageBox.Show(message);
        }
    }
}
